<template>
  <div class="pass">
    <!-- 确诊患者管理 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>确诊患者管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <div class="search">
        <!--两个按钮 -->
        <el-button type="info" @click="getPatientAdd">新建</el-button>
        <el-button type="info" @click="reset">刷新</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="patientList">
        <el-table-column prop="base.uname" label="姓名"></el-table-column>
        <!-- 快速复制某行 alt+shift+剪头 -->
        <el-table-column prop="base.idCard" label="身份证号"></el-table-column>
        <el-table-column prop="base.gender" label="性别"></el-table-column>
        <el-table-column prop="base.address" label="住址"></el-table-column>
        <el-table-column
          prop="patient.onsetDate"
          label="住院日期"
        ></el-table-column>
        <el-table-column prop="base.phone" label="电话"></el-table-column>

        <el-table-column label="当前状态">
          <!-- 可以通过scope.row 拿到当前这一行数据 -->
          <template v-slot="scope">
            <!-- 0 待审核 1通过 2不通过 -->
            <el-tag v-if="scope.row.patient.status == 0" type="warning"
              >未处理</el-tag
            >
            <el-tag v-if="scope.row.patient.status == 1" type="success"
              >治愈</el-tag
            >
            <el-tag v-if="scope.row.patient.status == 2" type="danger"
              >死亡</el-tag
            >
          </template>
        </el-table-column>
        <!-- 只有当状态为待审核，也就是0时，才展示两个按钮 -->
        <el-table-column label="操作" width="300px">
          <template v-slot="scope">
            <div class="operation" v-if="scope.row.patient.status == 0">
              <!-- <el-button type="primary" @click="changeStatus(scope.row.id, 1),getPatientInfo()"
                >详情</el-button
              > -->
              <el-button
                color="green"
                @click="changeStatus(scope.row.patient.id, 1), insertCure(scope.row.base.id)"
                >转为治愈</el-button
              >
              <el-button
                type="danger"
                @click="changeStatus(scope.row.patient.id, 2), insertDead(scope.row.base.id)"
                >转为死亡</el-button
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column>
          <template v-slot="scope">
            <div>
              <el-button
                type="warning"
                @click="deletePatient(scope.row.patient.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="paging"
        background
        layout="prev, pager, next"
        :total="1000"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
// getAllPassAPi获取所有通行证 发请求
import { ElMessage } from "element-plus";
import {
  getAllPassAPi,
  deletePassApi,
  changePassStatusApi,
  getUserPassApi,
} from "../api/index.js";
import {
  getAllPatientAPI,
  changePatientStatusApi,
  deletePatientApi,
  addCureApi,
  addDeadApi
} from "../api/index1.js";
export default {
  data() {
    return {
      passList: [],
      searchById: "",
      patientList: [],
    };
  },
  methods: {
    // 1.获取病人列表的方法
    getPatientList() {
      getAllPatientAPI().then((res) => {
        console.log(res);
        this.patientList = res.data;
      });
    },
    // 2.根据id删除 patient
    deletePatient(id) {
      deletePatientApi(id).then((res) => {
        // console.log(res);
        // 1.刷新数据
        this.getPatientList();
        // 2.提示用户删除成功
        if (res.code == 20000) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 3.改变确诊患者状态
    changeStatus(id, status) {
      // console.log(id +'--'+ status);

      let obj = {
        id: id,
        status: status,
      };
      changePatientStatusApi(obj).then((res) => {
        console.log(res);
        if (res.code == 20000) {
          this.getPatientList();
          ElMessage({
            message: "更新成功",
            type: "success",
          });
        }
      });
    },
    // 4.跳转
    getPatientAdd() {
      this.$router.push({
        path: "../views/patientAdd", //要跳转的页面的路由
      });
    },
    getPatientInfo() {
      this.$router.push({
        path: "../views/patientInfo", //要跳转的页面的路由
      });
    },
    // 5.重置
    reset() {
      this.searchById = "";
      this.getPatientList();
    },
    //6.插入治愈表的方法 addCureApi
    insertCure(baseid) {
      let obj={
        id:baseid
      }
      addCureApi(obj).then((res) => {
        console.log(res);
        if (res.code == 20000) {
          // this.getPatientList();
          ElMessage({
            message: "插入成功",
            type: "success",
          });
        }
      });
    },
    //7.插入死亡表的方法 addDeadApi
    insertDead(baseid) {
      let obj={
        id:baseid
      }
      addDeadApi(obj).then((res) => {
        console.log(res);
        if (res.code == 20000) {
          ElMessage({
            message: "插入成功",
            type: "success",
          });
        }
      });
    },
  
  
  },
  created() {
    this.getPatientList();
  },
};
</script>

<style lang="less" scoped>
.pass {
  .el-breadcrumb {
    margin-bottom: 30px;
  }
}
.search {
  width: 600px;
  display: flex;
  .el-input {
    margin-right: 30px;
  }
}
.operation {
  display: flex;
}
</style>